<template>
  <div class="antiBrush">
    <el-form ref="Form" :model="details" label-width="170px">
      <el-form-item label="投票验证码">
        <el-switch
          @change="handleVerificationCodeEnabled"
          v-model="details.verificationCodeEnabled"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="智能防刷">
        <el-switch
          @change="handleAntiBrushEnabled"
          v-model="details.antiBrushEnabled"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="可投票地区" class="select_wrap">
        <el-cascader
          v-model="value1"
          :options="provinceList"
          @change="handleChange"
        ></el-cascader>
        <!-- <el-select
          v-model="details.votingArea"
          @change="handleSelectArea"
          placeholder="请选择"
        >
          <el-option
            v-for="item in provinceList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select> -->
      </el-form-item>
      <el-form-item label="微信黑名单防刷">
        <div class="heiList_wrap">
          <div
            :class="{ active: currentIndex === index }"
            class="item"
            v-for="(item, index) in heilist"
            @click="handleHei(item, index)"
            :key="index"
          >
            {{ item.name }}
          </div>
        </div>
      </el-form-item>
      <el-form-item label="限制每天投票时间段" class="time_wrap">
        <div class="limit">
          <div>
            <span>开启</span>
            <el-switch
              @change="handleLimitDailyVotingTimeEnabled"
              v-model="details.limitDailyVotingTimeEnabled"
            >
            </el-switch>
          </div>
          <div class="time">
            <el-time-picker
              value-format="HH:mm"
              :clearable="false"
              :disabled="!details.limitDailyVotingTimeEnabled"
              v-model="details.limitDailyVotingTimeOnBeginValue"
              placeholder="7:00"
              format="HH:mm"
              @change="handleBegin"
            >
            </el-time-picker>
            <span>一</span>
            <el-time-picker
              format="HH:mm"
              :clearable="false"
              value-format="HH:mm"
              :disabled="!details.limitDailyVotingTimeEnabled"
              v-model="details.limitDailyVotingTimeOnEndValue"
              placeholder="23:00"
              @change="handleEnd"
            >
            </el-time-picker>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="封禁投票IP">
        <el-input
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6 }"
          placeholder="请输入要封禁的IP，一行一个IP"
          v-model="IP"
          @change="handleIP"
        >
        </el-input>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// import { formatDate } from "@/utils/filter";
export default {
  data() {
    return {
      voteId: Number(this.$route.query.id),
      details: {},
      value1:[],
      IP: "",
      provinceList: [],
      heilist: [
        {
          name: "低",
          value: "LOW",
        },
        {
          name: "中",
          value: "MEDIUM",
        },
        {
          name: "高",
          value: "HIGH",
        },
        {
          name: "关闭",
          value: "OFF",
        },
      ],
      currentIndex: "",
    };
  },
  created() {
    this.getSecurity();
    this.getArea();
  },
  methods: {
    handleChange(value){
        console.log(value)
        console.log(this.value1)
        this.saveApi();
    },
    handleHei(item, index) {
      this.currentIndex = index;
      this.details.wechatBlacklistAntiSwipe = item.value;
      this.saveApi();
    },
    getArea() {
      this.$api("voteSet.province").then((res) => {
        console.log(res)
        // const arr = [];
        // for (let k in res) {
        //   arr.push({
        //     value: k,
        //     label: res[k],
        //   });
        // }
        this.provinceList = res
        console.log(this.provinceList)
        
      });
    },
    getSecurity() {
      this.$api("voteSet.getSecurity", {
        voteId: this.voteId,
      }).then((res) => {
        console.log(res);
        this.details = res;
        this.value1=[res.votingArea,res.votingAreaCity]
        // this.details.limitDailyVotingTimeOnBeginValue = "07:00";
        // this.details.limitDailyVotingTimeOnEndValue = "07:02";
        // let time1 = this.details.limitDailyVotingTimeOnBeginValue.split(":");
        // this.details.limitDailyVotingTimeOnBeginValue = new Date(
        //   new Date().getFullYear(),
        //   new Date().getMonth(),
        //   new Date().getDate(),
        //   Number(time1[0], Number(time1[1]), Number(time1[2]))
        // );
        // let time2 = this.details.limitDailyVotingTimeOnEndValue.split(":");
        // this.details.limitDailyVotingTimeOnEndValue = new Date(
        //   new Date().getFullYear(),
        //   new Date().getMonth(),
        //   new Date().getDate(),
        //   Number(time2[0], Number(time2[1]), Number(time2[2]))
        // );
        console.log(this.details);
        this.currentIndex = this.heilist.findIndex(
          (item) => item.value == this.details.wechatBlacklistAntiSwipe
        );
      });
    },
    handleIP() {
      let strContent = "";
      strContent = this.IP.replace(/\r\n/g, ","); //IE9、FF、chrome
      strContent = this.IP.replace(/\n/g, ","); //IE7-8
      strContent = this.IP.replace(/\s/g, ","); //空格处理
      let arr = strContent.split(",");
      this.details.banVotingIp = arr.filter((item) => item);
      console.log(this.details.banVotingIp);
      this.saveApi();
    },
    saveApi() {
      //处理时间格式
      this.$api("voteSet.saveSecurity", {
        ...this.details,
        votingArea:this.value1[0],
        votingAreaCity:this.value1[1],
        // limitDailyVotingTimeOnBeginValue: formatDate(
        //   this.details.limitDailyVotingTimeOnBeginValue,
        //   "HH:mm:ss"
        // ),
        // limitDailyVotingTimeOnEndValue: formatDate(
        //   this.details.limitDailyVotingTimeOnEndValue,
        //   "HH:mm:ss"
        // ),
        voteId: this.voteId,
      }).then((res) => {
        console.log(res);
        this.$store.commit("status/SET_URL", new Date().getTime());
        console.log(res);
        this.$message({
          message: "保存成功",
          type: "success",
          duration: 1000,
        });
      });
    },
    handleVerificationCodeEnabled() {
      this.saveApi();
    },
    handleBegin() {
      this.saveApi();
    },
    handleEnd() {
      this.saveApi();
    },
    handleLimitDailyVotingTimeEnabled() {
      this.saveApi();
    },
    handleAntiBrushEnabled() {
      this.saveApi();
    },
    handleWechatBlacklistAntiSwipe() {
      this.saveApi();
    },
    handleSelectArea() {
      this.saveApi();
    },
  },
};
</script>

<style lang="scss" scoped>
.antiBrush {
  width: 820px;
  min-height: 550px;
  .select_wrap {
    ::v-deep.el-input__inner {
      height: 35px;
      line-height: 35px;
    }
  }
  .time_wrap {
    ::v-deep.el-date-editor {
      height: 30px;
      line-height: 30px;
      width: 100px;
      margin-top: 5px;
      .el-input__inner {
        height: 30px;
        line-height: 30px;
      }
    }
    ::v-deep.el-input__prefix {
      display: none;
    }
  }
  .heiList_wrap {
    display: flex;
    align-items: center;
    border-radius: 30px;
    background-color: rgb(223, 224, 229);
    width: 230px;
    margin-top: 5px;

    .item {
      height: 30px;
      flex: 1;
      text-align: center;
      line-height: 30px;
      color: #fff;
    }
    .active {
      background-color: $themeColor;
      border-radius: 30px;
    }
  }
  ::v-deep.el-form-item__label {
    padding-right: 40px;
  }
  .outerBox {
    width: 250px;
    height: 40px;
    display: flex;
    align-items: center;
    .showBox {
      border-radius: 30px;
      background-color: #dbdbdb;
      width: 100%;
      height: 30px;
      display: flex;
      color: #fff;
      font-weight: 600;
      .active {
        background-color: $themeColor;
      }
      .blacklist {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 50%;
        height: 100%;
        border-radius: 30px;
        cursor: pointer;
      }
    }
  }
  .limit {
    display: flex;

    span {
      color: $deactiveColor;
      margin-right: 10px;
    }
    .time {
      display: flex;
      margin-left: 120px;
      span {
        margin: 0 10px;
      }
    }
  }
  .forbid {
    .el-input {
      width: 32%;
    }
    ::v-deep.el-input__inner {
      height: 30px;
      width: 140px;
      text-align: center;
    }
  }
}
</style>